package com.rongzhe.house.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.User;
import com.rongzhe.house.manager.ConfigManager;
import com.rongzhe.house.presenter.UserInfoPresenter;
import com.rongzhe.house.ui.view.XCRoundRectImageView;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huang on 2017/11/20.
 */

public class UserInfoActivity extends BaseActivity<UserInfoPresenter> {
    @BindView(R.id.image_head)
    XCRoundRectImageView imageHead;
    @BindView(R.id.text_nick_name)
    TextView textNickName;
    @BindView(R.id.text_authentication_state)
    TextView textAuthenticationState;
    @BindView(R.id.text_do_authentication)
    TextView textDoAuthentication;

    @Override
    protected UserInfoPresenter createPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.image_head)
    public void onChooseImageClick() {
        mPresenter.startChooseImage();
    }

    @OnClick(R.id.text_logout)
    public void onLogoutClick() {
        mPresenter.logout();
    }

    @OnClick(R.id.text_do_authentication)
    public void onDoAuthenClick() {
        Intent intent = new Intent(this, UserAuthActivity.class);
        launchActivity(true, intent);
    }

    @OnClick(R.id.text_about_us)
    public void onAboutUsClick() {
        String url = ConfigManager.getInstance().getAboutUsUrl();
        if(TextUtils.isEmpty(url)) {
            showToast("something is wrong, can not get url");
        }
        Intent aboutUsIntent = H5BaseActivity.getH5BaseActivityIntent(this,
                getString(R.string.about_us), url);
        launchActivity(true, aboutUsIntent);
    }

    public void showUserInfo(User data) {
        if (data == null) {
            return;
        }
        textNickName.setText(data.getNickname());
        if (!TextUtils.isEmpty(data.getAvatar())) {
            setImgHead(data.getAvatar());
        }
    }

    public void setIsAuthed(int authed) {
        //TODO waht means of 1, 2
        if (authed == 1 || authed == 2) {
            textAuthenticationState.setTextColor(getResources().getColor(R.color.font_color_gray_2));
            textAuthenticationState.setText(authed == 2 ? getString(R.string.authed)
                    : getString(R.string.in_auth));
            textDoAuthentication.setEnabled(false);
        } else {
            textAuthenticationState.setTextColor(getResources().getColor(R.color.font_color_em));
            textAuthenticationState.setText(authed == 3 ? getString(R.string.auth_fail)
                    : getString(R.string.no_auth));
            textDoAuthentication.setEnabled(true);
        }
    }

    @Override
    protected String getActionTitle() {
        return getString(R.string.user_info);
    }

    public void setImgHead(String url) {
        Picasso.with(this).load(url).into(imageHead);
    }

}
